# _*_ coding: utf-8 _*_
__author__ = 'yinlei'
__date__ = '2018-05-21 11:35'
import re
from string import ascii_letters
from string import digits
import random
from exts import mail
from flask_mail import Message
from utils import restful,zlcache


def validateEmail(email):

    if len(email) > 7:
        if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
            return True
    return False

def send_captcha(email,msg,subject):
    random_str = ascii_letters+digits
    captcha = ''.join(random.sample(random_str,8))
    message = Message(subject=subject,body=msg.format(captcha),recipients=[email,])
    try:
        mail.send(message)
    except:
        return restful.server_error()
    zlcache.set(email,captcha)
    return restful.success('发送成功')

def random_string(length=6):
    random_str =ascii_letters+digits
    captcha = ''.join(random.sample(random_str,length))
    return captcha